var t = getApp()
Page({
  data: {
    site:'',
    store:'',
    info:'',
    setInter:'',
    getInfo:'',
    store_info:'',
    navHeight:t.globalData.navHeight,
     // 获取手机号弹窗参数
     show: false,
     duration: 300, //动画时长，单位毫秒
     position: 'bottom',
     round: true,  //是否显示圆角
     overlay: true, //是否显示遮罩层
     customStyle: '',//自定义弹出层样式
     overlayStyle: '' //自定义遮罩层样式
  },
  onLoad: function (options) {
    console.log(options);
    var that = this
    if (options) {
      this.setData({
        site:options.site,
        store:options.store
      })
    }else{
      wx.switchTab({
        url: '/pages/index/index',
      })
      return
    }
  },
  onShow(e){
    var that = this
    wx.login({
      success: res => {
        t.request('/login',{code:res.code},function(r){
          console.log(r);
         
            t.request('/users/get_store_balance',{store:that.data.store,site:that.data.site},function(e){
              console.log(e);
              if (e.code==200) {
                that.setData({
                  balance:e.result.balance,
                  store_info:e.result.store,
                })
              }else if(e.code==400){
                wx.showToast({
                  title: e.msg,
                  icon:'none'
                })
                wx.switchTab({
                  url: '/pages/index/index',
                })
              }
            })
          // 先判断是否绑定电话
          if(r.result.userInfo.get_phone !=1){
            that.showTelLogin()
          }
          // 判断是否有车辆
          if (r.result.userInfo.cars<1) {
            that.login = that.selectComponent("#login")
            that.login.show_login()
          }
          // if(r.result.userInfo.user_id > 0){
          //   wx.setStorageSync('userinfo',r.result.userInfo);
          // }
        })
      }
    })
    //判断手机是否登录了
    // if (t.globalData.show_tel_login) {
    //   console.log("手机未登录")
    //   this.showTelLogin()
    //   return;
    // }
  },
  // 显示电话登录的弹窗
  showTelLogin(){
    this.setData({show: true})
  },
  // 手机号 暂不登录按钮
  exit() {
    this.setData({show: false})
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  //电话登录
  getPhoneNumber(e){
    const that = this,
      iv = e.detail.iv,
      da = e.detail.encryptedData;
    if (!e.detail.encryptedData) return wx.showToast({ title: '授权失败,请重新授权！',icon:'none'})
    // 请求接口
    wx.login({
      success(e){
        console.log(e);
        wx.showLoading({
          title: '请稍后',
        })
        t.request('/login/getPhoneNumber',{code: e.code,iv:iv,encryptedData:da},function(event){
          wx.hideLoading({})
          if (event.code!==200) return wx.showToast({ title: event.msg,icon:'none' })
          if (event.code==200) {
            wx.setStorageSync('tel', event.result)
            console.log('手机登录成功')
            t.globalData.show_tel_login =false
            that.setData({show: false})
          }
        })
      }
    })
  },
  invest(e){
    var that = this
    wx.navigateTo({
      url: '/pages/personCenter/chooseStore/invest/invest?store_id=' + that.data.store,
    })
  },
  open_gate(e){
    var that = this
    wx.showLoading({title: '请稍后',})
    t.request('/StoreSite/start_order',{store:that.data.store,site:that.data.site},function(e){
      console.log(e);
      wx.hideLoading({success: (res) => {},})
      if (e.code==200) {
        wx.showToast({
          title: e.msg,
        })
        wx.navigateTo({
          url: '/pages/index/index'
        })
      }else{
        wx.showToast({
          title: e.msg,
          icon:'none'
        })
      }
    })
  }
})